﻿using ControlzEx.Theming;
using LearnWPF.Library.Logger;
using Stylet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace LearnWPF.Pages
{
    public class MainViewModel : Conductor<IScreen>.Collection.OneActive
    {
        private readonly IWindowManager _windowManager;
        private readonly IViewFactory _viewFactory;
        private readonly ILogger _logger;

        public MainViewModel(IWindowManager windowManager, IViewFactory viewFactory, ILogger logger)
        {
            _windowManager = windowManager;
            _viewFactory = viewFactory;
            _logger = logger;
        }

        protected override void OnInitialActivate()
        {
            base.OnInitialActivate();
            InitThemeSetting();
            InitMenuItemList();
        }

        #region Setting
        public bool IsSettingFlyoutOpen { get; set; }
        public void DoSetting()
        {
            IsSettingFlyoutOpen = !IsSettingFlyoutOpen;
        }
        #endregion

        #region 主题

        private string BackgroundName;

        public bool ThemeBackgroundDarkChecked { get; set; }
        public bool ThemeBackgroundLightChecked { get; set; }

        public List<AccentColorData> AccentColors { get; set; }
        public AccentColorData SelectedAccentColor { get; set; }

        private void InitThemeSetting()
        {
            BackgroundName = "Light";
            ThemeBackgroundDarkChecked = false;
            ThemeBackgroundLightChecked = !ThemeBackgroundDarkChecked;

            AccentColors = ThemeManager.Current.Themes
                                          .GroupBy(x => x.ColorScheme)
                                          .OrderBy(a => a.Key)
                                          .Select(a => new AccentColorData { Name = a.Key, ColorBrush = a.First().ShowcaseBrush })
                                          .ToList();

            SelectedAccentColor = new AccentColorData { Name = "Cyan" };

            this.Bind(s => SelectedAccentColor, (o, e) => AccentColorChanged());
        }

        public void ThemeBackgroundLightClick()
        {
            BackgroundName = "Light";
            ThemeManager.Current.ChangeTheme(Application.Current, $"{BackgroundName}.{SelectedAccentColor.Name}");
        }

        public void ThemeBackgroundDarkClick()
        {
            BackgroundName = "Dark";
            ThemeManager.Current.ChangeTheme(Application.Current, $"{BackgroundName}.{SelectedAccentColor.Name}");
        }

        public void AccentColorChanged()
        {
            ThemeManager.Current.ChangeTheme(Application.Current, $"{BackgroundName}.{SelectedAccentColor.Name}");
        }

        public class AccentColorData
        {
            public string Name { get; set; }
            public Brush ColorBrush { get; set; }
        }

        #endregion

        #region 菜单

        public class MenuItem
        {
            public string Title { get; set; }
        }

        public List<MenuItem> MenuItemList { get; set; }
        public int SelectedIndex { get; set; } = 0;

        private void InitMenuItemList()
        {
            MenuItemList = new List<MenuItem>()
            {
                new MenuItem(){Title="常用控件"},
                new MenuItem(){Title="布局"},
                new MenuItem(){Title="WPF基本特性"},
                new MenuItem(){Title="模板"},
                new MenuItem(){Title="控件"},
                new MenuItem(){Title="报表(DataGird)"},
                new MenuItem(){Title="下拉框与枚举类型"},
                //new MenuItem(){Title="Test"},
            };

            this.Bind(s => SelectedIndex, (o, e) => SelectedIndexChanged());
            SelectedIndexChanged();
        }

        private void SelectedIndexChanged()
        {
            switch (SelectedIndex)
            {
                case 0: ActivateItem(PageCommonControlsView ??= _viewFactory.PageCommonControlsViewModel()); break;
                case 1: ActivateItem(PageLayoutView ??= _viewFactory.PageLayoutViewModel()); break;
                case 2: ActivateItem(PageBasicCharacteristicView ??= _viewFactory.PageBasicCharacteristicViewModel()); break;
                case 3: ActivateItem(PageTemplateView ??= _viewFactory.PageTemplateViewModel()); break;
                case 4: ActivateItem(PageControlsView ??= _viewFactory.PageControlsViewModel()); break;
                case 5: ActivateItem(PageDataGridView ??= _viewFactory.PageDataGridViewModel()); break;
                case 6: ActivateItem(PageComboBoxView ??= _viewFactory.PageComboBoxViewModel()); break;
                case 7: ActivateItem(PageTestView ??= _viewFactory.PageTestViewModel()); break;
            }
        }

        private PageCommonControlsViewModel PageCommonControlsView;
        private PageLayoutViewModel PageLayoutView;
        private PageBasicCharacteristicViewModel PageBasicCharacteristicView;
        private PageTemplateViewModel PageTemplateView;
        private PageControlsViewModel PageControlsView;
        private PageDataGridViewModel PageDataGridView;
        private PageComboBoxViewModel PageComboBoxView;
        private PageTestViewModel PageTestView;

        #endregion
    }
}
